package com.ruoyi.production.service.impl;

import cn.hutool.core.io.resource.ClassPathResource;
import cn.hutool.core.io.resource.ResourceUtil;
import cn.hutool.extra.qrcode.QrCodeUtil;
import cn.hutool.extra.qrcode.QrConfig;
import com.alibaba.fastjson2.JSON;
import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.common.core.domain.entity.SysUser;
import com.ruoyi.common.exception.ServiceException;
import com.ruoyi.common.utils.DateUtils;
import com.ruoyi.common.utils.SecurityUtils;
import com.ruoyi.production.domain.WorkReport;
import com.ruoyi.production.domain.WorkReportMember;
import com.ruoyi.production.mapper.WorkReportMapper;
import com.ruoyi.production.mapper.WorkReportMemberMapper;
import com.ruoyi.production.service.IWorkReportMemberService;
import com.ruoyi.production.vo.CompomentQrVO;
import com.ruoyi.yard.domain.CompWarehouseInfo;
import com.ruoyi.yard.mapper.CompWarehouseInfoMapper;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import javax.imageio.ImageIO;
import javax.servlet.http.HttpServletResponse;
import java.awt.*;
import java.awt.image.BufferedImage;
import java.io.File;
import java.io.FileInputStream;
import java.io.IOException;
import java.io.InputStream;
import java.net.URISyntaxException;
import java.net.URL;
import java.util.List;

/**
 * 今日产出Service业务层处理
 *
 * @author ruoyi
 * @date 2022-09-05
 */
@Service
public class WorkReportMemberServiceImpl implements IWorkReportMemberService {
    @Autowired
    private WorkReportMemberMapper workReportMemberMapper;

    @Autowired
    private WorkReportMapper workReportMapper;

    @Autowired
    private CompWarehouseInfoMapper compWarehouseInfoMapper;

    /**
     * 查询今日产出
     *
     * @param id 今日产出主键
     * @return 今日产出
     */
    @Override
    public WorkReportMember selectWorkReportMemberById(Long id) {
        return workReportMemberMapper.selectWorkReportMemberById(id);
    }

    /**
     * 查询今日产出列表
     *
     * @param workReportMember 今日产出
     * @return 今日产出
     */
    @Override
    public List<WorkReportMember> selectWorkReportMemberList(WorkReportMember workReportMember) {
        return workReportMemberMapper.selectWorkReportMemberList(workReportMember);
    }

    /**
     * 新增今日产出
     *
     * @param workReportMember 今日产出
     * @return 结果
     */
    @Override
    public int insertWorkReportMember(WorkReportMember workReportMember) {
        return workReportMemberMapper.insertWorkReportMember(workReportMember);
    }

    /**
     * 修改今日产出
     *
     * @param workReportMember 今日产出
     * @return 结果
     */
    @Override
    public int updateWorkReportMember(WorkReportMember workReportMember) {
        return workReportMemberMapper.updateWorkReportMember(workReportMember);
    }

    /**
     * 批量删除今日产出
     *
     * @param ids 需要删除的今日产出主键
     * @return 结果
     */
    @Override
    public int deleteWorkReportMemberByIds(Long[] ids) {
        return workReportMemberMapper.deleteWorkReportMemberByIds(ids);
    }

    /**
     * 删除今日产出信息
     *
     * @param id 今日产出主键
     * @return 结果
     */
    @Override
    public int deleteWorkReportMemberById(Long id) {
        return workReportMemberMapper.deleteWorkReportMemberById(id);
    }

    @Override
    @Transactional(rollbackFor = {Exception.class})
    public int warehousing(Long id){
        //获取构件信息
        WorkReportMember workReportMember = workReportMemberMapper.selectWorkReportMemberById(id);
        if (workReportMember == null){
            throw new ServiceException("未获取到构件信息!");
        }
        WorkReport workReport = workReportMemberMapper.getWorkReport(workReportMember.getWorkReportNo());
        CompWarehouseInfo compWarehouseInfo = workReportMemberMapper.getCompWarehouseInfo(workReportMember.getCompomentNo());
        if (compWarehouseInfo == null){
            compWarehouseInfo.setWarehouseCompomentNo(workReportMember.getCompomentNo());
            compWarehouseInfo.setProjectNo(workReport.getProjectNo());
            compWarehouseInfo.setBuildNo(workReportMember.getBuildNo());
            compWarehouseInfo.setFloor(workReportMember.getFloor());
            compWarehouseInfo.setStockCount(workReportMember.getOutputQuantity());
            SysUser user = SecurityUtils.getLoginUser().getUser();
            compWarehouseInfo.setWarehousedBy(user.getUserName());
            compWarehouseInfo.setWarehousedTime(DateUtils.getNowDate());
            return compWarehouseInfoMapper.updateCompWarehouseInfo(compWarehouseInfo);
        }else {
            compWarehouseInfo.setStockCount(compWarehouseInfo.getStockCount() + workReportMember.getOutputQuantity());
            return compWarehouseInfoMapper.insertCompWarehouseInfo(compWarehouseInfo);
        }
    }

    @Override
    @Transactional
    public void printQRcode(HttpServletResponse response,Long id) throws IOException, URISyntaxException {
        //获取构件信息
        WorkReportMember workReportMember = workReportMemberMapper.selectWorkReportMemberById(id);
        if (workReportMember == null){
            throw new ServiceException("未获取到构件信息!");
        }
        if (workReportMember.getWhetherStock() != "1"){
            throw new ServiceException("构件未入库信息!");
        }
        WorkReport workReport = workReportMemberMapper.getWorkReport(workReportMember.getWorkReportNo());
        if (workReport == null){
            throw new ServiceException("为查询工单信息!");
        }

        CompomentQrVO compomentQrVO = CompomentQrVO.getCompomentQrVO(workReportMember.getCompomentNo(),workReport.getProjectName(), workReportMember.getCompomentName(), workReportMember.getBuildNo(),
                workReportMember.getFloor(),String.valueOf(workReportMember.getNetWeight()),workReportMember.getSpecifications(),String.valueOf(workReportMember.getLength()));
        String logoPath = (new ClassPathResource("image" + File.separator + "logo.jpg")).getAbsolutePath();
        String uopath = (new ClassPathResource("image" + File.separator + "u0.png")).getAbsolutePath();
        BufferedImage generate = QrCodeUtil.generate(compomentQrVO.toString(), QrConfig.create().setImg(logoPath));
        BufferedImage u0Image = loadImageLocal(uopath);
        Graphics2D graphics = u0Image.createGraphics();
        graphics.drawImage(generate, 450, 150, null);
        graphics.setRenderingHint(RenderingHints.KEY_TEXT_ANTIALIASING, RenderingHints.VALUE_TEXT_ANTIALIAS_ON);
        Font font = new Font("微软雅黑", Font.BOLD, 30);// 添加字体的属性设置
        graphics.setFont(font);
        Color color = new Color(0, 0, 0);
        graphics.setColor(color);
        int height = 750;
        for (String s : compomentQrVO.getList()) {
            graphics.drawString(s, 95, height);
            height += 50;
        }
        graphics.dispose();
        ImageIO.write(u0Image, "png", response.getOutputStream());
        workReportMember.setWhetherPrint("1");
        workReportMemberMapper.updateWorkReportMember(workReportMember);
        CompWarehouseInfo compWarehouseInfo = workReportMemberMapper.getCompWarehouseInfo(workReportMember.getCompomentNo());
        if (StringUtils.isBlank(compWarehouseInfo.getQrCode())){
            compWarehouseInfo.setQrCode(compomentQrVO.getBase64());
            compWarehouseInfoMapper.updateCompWarehouseInfo(compWarehouseInfo);
        }

    }
    private static BufferedImage loadImageLocal(String uopath) {
        try {
            return ImageIO.read(new File(uopath));
        } catch (Exception e) {

        }
        return null;
    }
}
